package com.example.front.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.front.R;
import com.example.front.main.shop.ProductActivity;
import com.example.front.main.shop.SelectProduct;

import java.util.List;

public class ShopAdapter extends BaseAdapter {
    private List<SelectProduct> productList;
    private Context context;
    private View.OnClickListener addNumber;
    private View.OnClickListener subtractNumber;

    private void setAddNumber(View.OnClickListener addNumber){
        this.addNumber = addNumber;
    }

    private void setSubtractNumber(View.OnClickListener subtractNumber){
        this.subtractNumber = subtractNumber;
    }

    public ShopAdapter(List<SelectProduct> productList, Context context){
        this.productList = productList;
        this.context = context;
    }

    @Override
    public int getCount() {
        if(productList != null)return productList.size();
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return productList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = null;
        if(convertView != null)v = convertView;
        else v = LayoutInflater.from(context).inflate(R.layout.product_list_item, parent, false);

        ViewHolder holder = (ViewHolder)v.getTag();
        if(holder == null){
            holder = new ViewHolder();
            holder.shopImage = (ImageView)v.findViewById(R.id.image);
            holder.product_name_textView = (TextView)v.findViewById(R.id.name_textView);
            holder.product_price_textView = (TextView)v.findViewById(R.id.type_textView);
            holder.product_number_textView = (TextView)v.findViewById(R.id.product_number_textView);
            holder.add_button = (ImageView)v.findViewById(R.id.add_button);
            holder.subtract_button = (ImageView)v.findViewById(R.id.subtract_button);
        }

        Glide.with(context)
                .load(productList.get(position).getPhoto())
                .placeholder(R.drawable.loading)
                .error(R.drawable.error)
                .crossFade()
                .into(holder.shopImage);
        holder.product_name_textView.setText(productList.get(position).getName() + "");
        holder.product_price_textView.setText(" " + productList.get(position).getType() + " " + productList.get(position).getPrice() + "/" + productList.get(position).getUnit());
        holder.product_number_textView.setText(productList.get(position).getNumber() + "");
        holder.add_button.setTag(position);
        holder.subtract_button.setTag(position);
        v.setTag(holder);
        return v;
    }

    private static class ViewHolder{
        private ImageView shopImage;
        private TextView product_name_textView;
        private TextView product_price_textView;
        private TextView product_number_textView;
        private ImageView add_button;
        private ImageView subtract_button;
    }
}
